﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.Animation;
using Trippy.CollidableGameComponents.Agents;
using Trippy.CollidableGameComponents.Objects;
using Trippy.CollidableGameComponents.Events;
using Trippy.Levels;


namespace Trippy.CollidableGameComponents.Projectiles
{
    class Fireball : CollidableGameComponent
    {
        public static Texture2D SpriteSheet { get; private set; }

        protected int bounceCount = 0;

        public Fireball(Level level)
            : base(level)
        {
            if (SpriteSheet == null) SpriteSheet = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Projectiles/fireball");
            Sprite = new AnimatedSprite(this, SpriteSheet, SpriteSheet.Height, SpriteSheet.Width, 0, 0, 0, 0, 1, 0f);
            Friction = 0f;
        }

        protected bool bounced;
        public override void Update(GameTime gameTime)
        {
            bounced = false;
            UpdateStandardPhysics(TrippyUtilities.CalculateElapsedTime(gameTime));
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            StandardDraw(gameTime);
            base.Draw(gameTime);
        }

        protected void BounceX()
        {
             Velocity = new Vector2(-Velocity.X, Velocity.Y);
        }

        protected void BounceY()
        {
            Velocity = new Vector2(Velocity.X, -Velocity.Y);
        }

        protected override void CollisionHandler(CollidableGameComponent component, CollidableGameComponent.CollisionSide side)
        {
            if (!(component is Orb) && !(component is PowerUp) && !(component is Event))
            {
                if (side == CollidableGameComponent.CollisionSide.Bottom)
                {
                    BounceY();
                    SetBottomY(component.BoundingBox.Top);
                }
                if (side == CollidableGameComponent.CollisionSide.Top)
                {
                    BounceY();
                    SetTopY(component.BoundingBox.Bottom);
                }

                if (side == CollidableGameComponent.CollisionSide.Left)
                {
                    BounceX();
                    SetLeftX(component.BoundingBox.Right);
                }
                if (side == CollidableGameComponent.CollisionSide.Right)
                {
                    BounceX();
                    SetRightX(component.BoundingBox.Left);
                }
                if(bounced == false)
                {
                    bounceCount++;
                    bounced = true;
                    if (bounceCount > 8)
                    {
                        new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Explosion, Position);
                        MarkForDeletion();
                    }
                }
            }
            if (component is Enemy || component is PathingEnemy || component is SwoopingEnemy)
            {
                if (component.Sprite.Tint != Color.Red) //if enemy is not already dying
                {
                    component.Sprite.Tint = Color.Red;
                    component.MarkForDeletion(0.2f);
                }
                new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Explosion, Position);
                MarkForDeletion();                
            }
            if (component is SlaveOrb)
            {
                var slave = (SlaveOrb) component;

                new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Explosion, slave.Position);
                MarkForDeletion();
                
                slave.DoDamage(1);
            }
            if (component is Ian)
            {
                var ian = (Ian)component;

                new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Explosion, ian.Position);
                MarkForDeletion();

                ian.DoDamage(1);
            }


                base.CollisionHandler(component, side);
        }

    }
}
